package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.data.entity.ImageEntity;
import com.ja.openchat.ui.found.BasePagerGoChatCircleFragment;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;


public class GoChatCirclePublishViewModel extends BaseViewModel {
    private static final String MULTIRECYCLETYPE_IMAGE = "image";
    private static final String MULTIRECYCLETYPE_ADD = "add";
    public ObservableField<String> userText = new ObservableField<>("");
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();
    public ObservableList<MultiItemViewModel> observableListAt = new ObservableArrayList<>();
    private int max = 9;

    public GoChatCirclePublishViewModel(@NonNull Application application) {
        super(application);
        GoChatCirclePublishImageAddViewModel goChatCirclePublishImageViewModel = new GoChatCirclePublishImageAddViewModel(this, null);
        goChatCirclePublishImageViewModel.multiItemType(MULTIRECYCLETYPE_ADD);
        observableList.add(goChatCirclePublishImageViewModel);
        GoChatCirclePublishImageAddAtViewModel goChatCirclePublishImageAtViewModel = new GoChatCirclePublishImageAddAtViewModel(this, null);
        goChatCirclePublishImageAtViewModel.multiItemType(MULTIRECYCLETYPE_ADD);
        observableListAt.add(goChatCirclePublishImageAtViewModel);
    }

    public void addImage(ImageEntity imageEntity) {
        if (observableList.size() >= max) {
            observableList.remove(max - 1);
            GoChatCirclePublishImageViewModel goChatCirclePublishImageViewModel = new GoChatCirclePublishImageViewModel(this, imageEntity);
            goChatCirclePublishImageViewModel.multiItemType(MULTIRECYCLETYPE_IMAGE);
            observableList.add(max - 1, goChatCirclePublishImageViewModel);
            return;
        }
        GoChatCirclePublishImageViewModel goChatCirclePublishImageViewModel = new GoChatCirclePublishImageViewModel(this, imageEntity);
        goChatCirclePublishImageViewModel.multiItemType(MULTIRECYCLETYPE_IMAGE);
        observableList.add(observableList.size() - 1, goChatCirclePublishImageViewModel);
    }

    public void addImageAt(ImageEntity imageEntity) {
        GoChatCirclePublishImageAtViewModel goChatCirclePublishImageViewModel = new GoChatCirclePublishImageAtViewModel(this, imageEntity);
        goChatCirclePublishImageViewModel.multiItemType(MULTIRECYCLETYPE_IMAGE);
        observableListAt.add(observableListAt.size() - 1, goChatCirclePublishImageViewModel);
    }

    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            String itemType = (String) item.getItemType();
            if (MULTIRECYCLETYPE_IMAGE.equals(itemType)) {
                itemBinding.set(BR.viewModel, R.layout.item_multi_my_image_publish);
            } else if (MULTIRECYCLETYPE_ADD.equals(itemType)) {
                itemBinding.set(BR.viewModel, R.layout.item_multi_my_image_add);
            }
        }
    });

    public ItemBinding<MultiItemViewModel> itemBindingAt = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            String itemType = (String) item.getItemType();
            if (MULTIRECYCLETYPE_IMAGE.equals(itemType)) {
                itemBinding.set(BR.viewModel, R.layout.item_multi_my_image_publish_at);
            } else if (MULTIRECYCLETYPE_ADD.equals(itemType)) {
                itemBinding.set(BR.viewModel, R.layout.item_multi_my_image_add_at);
            }
        }
    });
    //点击事件
    public BindingCommand btnOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(BasePagerGoChatCircleFragment.class.getCanonicalName());
    });
}
